class SlaTemplateController < ApplicationController

  def create
    begin
      CloudController.logger.info("come in 1")
      @name = body_params[:name]
      @instances = body_params[:instances]
      @memsize = body_params[:memsize]
      @url = body_params[:url]
      @service = body_params[:service]
      @framework=body_params[:framework]
      @runtime=body_params[:runtime]
      @app_starttime = body_params[:app_starttime]
      @app_stoptime =body_params[:app_stoptime]

      CloudController.logger.info("come in 2")

      sla_template = ::SlaTemplate.new(:name =>@name , :instances => @instances,:memsize=>@memsize,:url=>@url,:framework=>@framework,:service=>@service,
      :runtime =>@runtime,:app_starttime=>@app_starttime, :app_stoptime=>@app_stoptime)

      CloudController.logger.info("sla_template :#{sla_template}")
      if sla_template.save
        render :json => {}
      else
        raise CloudError.new(CloudError::BAD_REQUEST)
      end
    end

  end

  def list

    CloudController.logger.info("come in cloudcontroller list")
      slatemplate_list = ::SlaTemplate.all.map do |sla_temlpate|
      slatemplate_hash = {:name =>sla_temlpate.name , :instances => sla_temlpate.instances,:memsize=>sla_temlpate.memsize,:url=>sla_temlpate.url,:framework=>sla_temlpate.framework,:service=>sla_temlpate.service,
                          :runtime =>sla_temlpate.runtime, :app_starttime=>sla_temlpate.app_starttime, :app_stoptime=>sla_temlpate.app_stoptime,
                          :created_at=>sla_temlpate.created_at,:updated_at=>sla_temlpate.updated_at}
       slatemplate_hash
    end
    CloudController.logger.info("slatemplate_list :#{slatemplate_list}")
      render :json => slatemplate_list
  end




  def delete

    @sla_template = ::SlaTemplate.find_by_name(params['name'])
    CloudController.logger.info("delete @sla_template=#{@sla_template.as_json}")
    @sla_template.destroy
    #render :json => {}
    render :status => 204, :nothing => true
  end

  def info
    @sla_template = ::SlaTemplate.find_by_name(params['name'])
    CloudController.logger.info("@sla_template=#{@sla_template}")
    if @sla_template
        slatemplate_info={:name =>@sla_template.name , :instances => @sla_template.instances,:memsize=>@sla_template.memsize,:url=>@sla_template.url,:framework=>@sla_template.framework,:service=>@sla_template.service,
                          :runtime =>@sla_template.runtime, :app_starttime=>@sla_template.app_starttime, :app_stoptime=>@sla_template.app_stoptime,
                          :created_at=>@sla_template.created_at,:updated_at=>@sla_template.updated_at}

        CloudController.logger.info("slatemplate_info=#{slatemplate_info}")
        render :json => slatemplate_info

    else
      raise CloudError.new(CloudError::SLA_NOT_FOUND)
    end


  end
 end